package com.yingdong.utils;

import java.util.HashMap;
import java.util.Map;

public class TrieTree {
	private Map<Character, TrieNode> root = new HashMap<>();

	public void addForbidenWord(String args) {
		Map<Character, TrieNode> map = root;
		for (int i = 0; i < args.length(); i++) {
			char c = args.charAt(i);
			if (map.get(c) == null) {
				map.put(c, new TrieNode(c));
			}
			map = map.get(c).getChildren();
		}
	}

	public boolean checkForbidenWord(String args) {
		for (int i = 0; i < args.length(); i++) {
			Map<Character, TrieNode> map = root;
			for (int j = i; j < args.length(); j++) {
				char c = args.charAt(j);
				if (!map.containsKey(c)) {
					break;
				} else if (map.get(c).getChildren().isEmpty()) {
					return false;
				}
				map = map.get(c).getChildren();
			}
		}
		return true;
	}

	public static void main(String[] args) {
		TrieTree tree = new TrieTree();
		 tree.addForbidenWord("我草你妈逼");
		 tree.addForbidenWord("我草你老爸");

		 System.out.println(tree.checkForbidenWord("retertertert我草你妈逼erterterert"));
	}
}
